home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
SGI Developer Toolbox 6.1
/
SGI Developer Toolbox 6.1 - Disc 1.iso
/
toolbox
/
src
/
exampleCode
/
opengl
/
extensions
/
samples
/
mkCurRd.c
< prev
next >
Wrap
C/C++ Source or Header
|
1996-11-11
|
12KB
|
357 lines
/*
* Copyright (c) 1994 Silicon Graphics, Inc.
*
* Permission to use, copy, modify, distribute, and sell this software and
* its documentation for any purpose is hereby granted without fee,
* provided that (i) the above copyright notices and this permission
* notice appear in all copies of the software and related documentation,
* and (ii) the name of Silicon Graphics may not be used in any
* advertising or publicity relating to the software without the specific,
* prior written permission of Silicon Graphics.
*
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
*
* IN NO EVENT SHALL SILICON GRAPHICS BE LIABLE FOR ANY SPECIAL,
* INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY
* DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
* OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
* LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
* OF THIS SOFTWARE.
*/
/*
*
* cc -o mkCurRd mkCurRd.c -lGLU -lGL -lX11 -lm
*
* Usage:
* mkCurRd [windId in hex] [-h/H for help]
* With out argument creates two windows and reads from one into the other.
* With argument windId uses windId as read window.
* Keys:
* a/A - increase/reduce copy speed
* c - do one copy
* C - Continous copy
* h/H/? - print key usage.
* When ran without an argument the following are also relevant
* f - flip read/write windows
* t - change the intensity of the image in the read window
*
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <GL/glx.h>
#include <GL/glu.h>
#include <X11/Xutil.h>
#include <X11/keysym.h>
#include "xwindow.h"
int widths[2], heights[2];
float clearColors[2][3];
Window wins[2];
Display *dpy;
float intensity = 1;
#ifndef GLX_SGI_make_current_read
/* Make The compiler barf */
GLX_SGI_make_current_read extension is not supported.
#else
void
changeIntensity(float delta)
{
if (intensity <= delta)
intensity = 1;
else
intensity -= delta;
}
void
drawWindow(int windex)
{
glClearColor(clearColors[windex][0],clearColors[windex][1],
clearColors[windex][2], 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glViewport(0, 0, widths[windex], heights[windex]);
/* draw a polygon */
glBegin(GL_QUADS);
glColor3f(intensity, 0.0, 0.0);
glVertex2f(-0.8,-0.8);
glColor3f(0.0, intensity, 0.0);
glVertex2f( 0.8,-0.8);
glColor3f(0.0, 0.0, intensity);
glVertex2f( 0.8, 0.8);
glColor3f(intensity, intensity, 0.0);
glVertex2f(-0.8, 0.8);
glEnd();
glDisable(GL_TEXTURE_2D);
/* draw an outline around the polygon */
glLineWidth(2.0);
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_LINE_LOOP);
glVertex2f(-0.8,-0.8);
glVertex2f( 0.8,-0.8);
glVertex2f( 0.8, 0.8);
glVertex2f(-0.8, 0.8);
glEnd();
glXSwapBuffers(dpy, wins[windex]);
}
static void
usage(char *name, char *extra, int status)
{
if (extra != NULL)
fprintf(stderr, "%s: %s\n", name, extra);
fprintf(stderr, "usage: %s [read windId in hex] [-h/H (for help)]\n", name);
fprintf(stderr,
"\tInteractively, `h/?' keys provide description of interface\n");
exit(status);
}
int
main(int argc, char *argv[])
{
int visualAttr[] = { GLX_RGBA, GLX_RED_SIZE, 1, GLX_DOUBLEBUFFER, None };
XEvent ev;
int drawWin, readWin;
GLXContext ctx;
int x = 0, y = 0, i;
int width = 300, height = 300;
int refreshWrite = True, refreshRead = True;
int copyWindow = False, continuous = False;
int myRead;
int xStart = 299, yStart = 299;
int speed = 1;
int zoom = 1;
const char *extensions;
if (argc > 1 && argv[1][0] == '-' &&
(argv[1][1] == 'h' || argv[1][1] == 'H')) {
usage(argv[0], NULL, EXIT_SUCCESS);
}
clearColors[0][1] = 1;
clearColors[0][0] = clearColors[0][2] = 0;
clearColors[1][0] = 1;
clearColors[1][1] = clearColors[0][2] = 0;
widths[0] = widths[1] = width;
heights[0] = heights[1] = height;
createWindowAndContext(&dpy, &wins[0], &ctx, x, y, width, height,
GL_FALSE, NULL, visualAttr, "MakeCurrentRead0");
if (argc > 1) {
wins[1] = strtol(argv[1], NULL, 16);
printf("Read window 0x%x\n", wins[1]);
myRead = False;
} else {
XSetWindowAttributes swa;
XWindowAttributes gwa;
myRead = True;
XGetWindowAttributes(dpy, wins[0], &gwa);
swa.background_pixel = None;
swa.border_pixel = None;
swa.colormap = gwa.colormap;
swa.event_mask = StructureNotifyMask | KeyPressMask | ExposureMask;
wins[1] = XCreateWindow(dpy, gwa.root,
x, y, width, height, 0,
gwa.depth, InputOutput, gwa.visual,
CWBackPixel | CWBorderPixel | CWColormap |
CWEventMask, &swa);
XStoreName(dpy, wins[1], "MakeCurrentRead1");
mapWindowAndWait(dpy, wins[1]);
}
/* Verify make_current_read extension support */
extensions = glXQueryExtensionsString(dpy, DefaultScreen(dpy));
if (extensions == NULL ||
strstr(extensions, "GLX_SGI_make_current_read") == NULL) {
fprintf(stderr, "The make_current_read extension\
is not supported on this system\n");
exit(EXIT_FAILURE);
}
/*
* Clear the window(s).
*/
for (i = (argc > 1) ? 0 : 1; i > -1; i--) {
int j;
for (j = 0; j < 2; j++) {
glXMakeCurrent(dpy, wins[i], ctx);
glClearColor(clearColors[i][0],clearColors[i][1],
clearColors[i][2], 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glXSwapBuffers(dpy, wins[i]);
}
}
glXMakeCurrentReadSGI(dpy, wins[0], wins[1], ctx);
glReadBuffer(GL_FRONT);
drawWin = 0;
readWin = 1;
while (1) {
while (XPending(dpy)) {
XNextEvent(dpy, &ev);
switch (ev.type) {
case KeyPress:
{
KeySym ks;
XLookupString(&ev.xkey, NULL, 0, &ks, NULL);
switch (ks) {
case XK_Escape:
exit(EXIT_SUCCESS);
break;
case XK_a:
if (speed < 50)
speed++;
else
fprintf(stderr, "Maximum speed reached \07\n");
break;
case XK_A:
if (speed > 1)
speed--;
break;
case XK_f:
if (argc < 2) {
drawWin = (drawWin + 1) & 0x1;
readWin = (drawWin + 1) & 0x1;
glXMakeCurrentReadSGI(dpy, wins[drawWin],
wins[readWin], ctx);
xStart = widths[readWin] - 1;
yStart = heights[readWin] - 1;
refreshWrite = !continuous;
} else {
fprintf(stderr,
"Flip not allowed for foreign windows\n");
}
break;
case XK_c:
if (xStart <= speed)
xStart = widths[readWin] - 1;
else
xStart -= speed;
if (yStart <= speed)
yStart = heights[readWin] - 1;
else
yStart -= speed;
copyWindow = True;
break;
case XK_C:
continuous = !continuous;
break;
case XK_z:
if (zoom < 4) {
zoom += 1;
glPixelZoom(zoom, zoom);
}
break;
case XK_Z:
if (zoom > 1) {
zoom -= 1;
glPixelZoom(zoom, zoom);
}
break;
case XK_t:
changeIntensity(0.1);
refreshRead = True;
break;
case XK_question:
case XK_h:
case XK_H:
printf("\nKeys:\n\
a/A - increase/reduce copy speed\n\
c - do one copy\n\
C - toggle continous copy\n\
z/Z - increase/reduce pixel zoom\n\
h/H/? - print key usage.\n\
When ran without an argument the following are also relevant\n\
f - flip read/write windows\n\
t - change the intensity of the image in the read window\n");
break;
default:
break;
}
}
break;
case ConfigureNotify:
width = ev.xconfigure.width;
height = ev.xconfigure.height;
if (ev.xconfigure.window == wins[drawWin]) {
widths[drawWin] = width;
heights[drawWin] = height;
refreshWrite = !continuous;
} else {
widths[readWin] = width;
heights[readWin] = height;
refreshRead = True;
}
break;
case Expose:
if (ev.xexpose.window == wins[drawWin])
refreshWrite = !continuous;
else
refreshRead = True;
break;
default:
break;
}
}
if (myRead && refreshRead) {
glXMakeCurrent(dpy, wins[readWin], ctx);
drawWindow(readWin);
refreshRead = False;
glXMakeCurrentReadSGI(dpy, wins[drawWin],
wins[readWin], ctx);
}
if (continuous) {
if (xStart <= speed)
xStart = widths[readWin] - 1;
else
xStart -= speed;
if (yStart <= speed)
yStart = heights[readWin] - 1;
else
yStart -= speed;
copyWindow = True;
}
if (copyWindow) {
int w = widths[readWin] - xStart;
int h = heights[readWin] - yStart;
if (w < 0)
w = widths[readWin];
if (h < 0)
h = heights[readWin];
glClearColor(clearColors[drawWin][0],clearColors[drawWin][1],
clearColors[drawWin][2], 1.0);
glClear(GL_COLOR_BUFFER_BIT);
glRasterPos2i(-1,-1);
glCopyPixels(xStart, yStart, widths[readWin] - xStart,
heights[readWin] - yStart, GL_COLOR);
copyWindow = False;
glXSwapBuffers(dpy, wins[drawWin]);
} else if (refreshWrite) {
drawWindow(drawWin);
refreshWrite = False;
}
#if 0
/* For debugging */
{
GLenum error;
while ((error = glGetError()) != GL_NO_ERROR) {
fprintf(stderr, "Error: %s\n",
(char *) gluErrorString(error));
}
}
#endif
}
}
#endif /* Make_current_read extension is not defined (else part) */